<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Login extends Controller
{
    /**
     * 登录
     *
     * @return \think\Response
     */
    public function index(Request$request)
    {
          if (\request()->isPost()){
              $info = $request->param();
              $rule = [
                  'user_name' => 'require',
                  'user_password' => 'require',
                  'code' => 'require'
              ];
              $msg = [
                  'user_name.require' => '账号不能为空',
                  'user_password.require' => '密码不能为空',
                  'code.require' => '验证码不能为空',
              ];
              $validate = new \think\Validate($rule,$msg);
              if (!$validate->check($info)) {
                  $error = $validate->getError();
                  return ['msg'=>$error];
              }
              //验证验证码
              if(!captcha_check($info['code'])){
                  // $this->error('验证码错误');
                  return ['msg' => '验证码错误'];
              };
              //把密码使用MD5简单加密
              $password = md5(md5($info['user_password']));
              $user=\app\admin\model\User::where(['user_name'=>$info['user_name'],'user_password'=>$password])->find();
              if ($user){

                  if ($user['user_status'] == '停用'){
                      return ['msg'=>'账号停用，请联系管理员！'];
                  }

                  //标识，跳转到公共页面
                  session('user_info',$user->toArray());

                  return ['msg' => '200'];

              }else{

                  return ['msg'=>'账号或者密码错误！'];
              }
              dump($info);

          }else{
              return view('login/login');
          }

    }





    /**
     * 退出
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function logout(){

        session(null);
        $this->redirect('admin/login/index');

    }

}
